﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SimpleIR;
using System.Diagnostics.Contracts;

namespace RgnAnalyzer.Domain
{
	/// <summary>
	/// Represent the index position of the pointer when processing a loop 
	/// </summary>
	internal static class AIndexPosition
	{
		[Flags]
		internal enum Value : byte
		{
			EmptyIntersect = 0x0,
			Before = 0x1,
			Current = 0x2,
			Unknown = 0XF
		}

		/// <summary>
		/// Return true if the two positions imply that the pointers do not alias.
		/// </summary>
		internal static bool PositionsGiveInjective(Value p1, Value p2)
		{ return (p1 & p2) == Value.EmptyIntersect; }

		/// <summary>
		/// Return the union of two index position values.
		/// </summary>
		internal static Value JoinPosition(Value p1, Value p2)
		{ return p1 | p2; }

		/// <summary>
		/// This indexes the currept position to before but does not fully advance to the next position (e.g. it is a half step forward).
		/// Current and Before -> before
		/// After -> After
		/// All other combinations become unknown
		/// </summary>
		internal static Value AdvanceIndexPosition(Value p)
		{
			if((p & (Value.Before | Value.Current)) == p)
				return Value.Before;
			else
				return Value.Unknown;
		}

		internal static string ConvertToString(Value p)
		{
			if(p == Value.EmptyIntersect)
				return "EmptyIntersect";
			else if(p == Value.Before)
				return "Before";
			else if(p == Value.Current)
				return "Current";
			else if(p == (Value.Before | Value.Current))
				return "Before|Current";
			else
				return "Unknown";
		}
	}
}
